package com.zy.control;

import com.zy.entity.BaseResponse;
import com.zy.entity.Book;
import com.zy.entity.Person;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@ApiIgnore
@RestController
@RequestMapping("/api")
@Api(value = "/api", description = "主要用于用户操作",tags = "用户管理相关接口")
@Slf4j
public class PersonController {


    @PostMapping("/v1/persons")
    @ApiOperation(value = "添加用户",notes = "主要是向后台添加数据",tags = {"person"}) //用于方法；表示一个http请求的操作
    @ApiResponses(value = { @ApiResponse(code = 1000, message = "成功"),
            @ApiResponse(code = 1001, message = "失败"),
            @ApiResponse(code = 1002, message = "缺少参数") })
    public String addPerson(@ApiParam(name = "person",required = true,value = "用户实体类") Person person) {
        return "person add suc!!";
    }

    @GetMapping("/v1/persons/{pid}")
    @ApiOperation(value = "查询用户", notes = "主要是根据ID查询用户",tags = {"person"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "用户id", defaultValue = "66", required = true, paramType = "String", example = "66")
    })
    public Person getPersonById(@PathVariable String pid, HttpServletRequest request) {
        String header = request.getHeader("token");
        log.info("****** header {}",header);

        if("1".equals(pid)){
            log.info("****** pid {}",pid);
            return new Person("p1","XIAOMIGN",1,new Date());
        }
        return null;
    }


    @GetMapping("/v2/persons/{pid}")
    @ApiOperation(value = "查询用户", notes = "主要是根据ID查询用户",tags = {"person"})
    @ApiResponses({
            @ApiResponse(code=200,message="成功",response= Book.class),
            @ApiResponse(code=401,message="输入参数错误",response=BaseResponse.class),
            @ApiResponse(code=500,message="系统内部错误",response=Book.class)
    })
    public ResponseEntity<Map<String,Object>> getPersonById2(@ApiParam(name = "pid", value = "需要删除的公告ids", required = true) @PathVariable String pid) {
        Map<String,Object> map = new HashMap<String,Object>();
        if("1".equals(pid)){
//            return  BaseResponse.<Person>builder()
//                    .code(200)
//                    .msg("查找成功")
//                    .data(Person.builder().pId("p1").pBirth(new Date()).pName("XIAOMIGN").pScore(1).build())
//                    .build();

            map.put("name", Person.builder().pId("p1").pBirth(new Date()).pName("XIAOMIGN").pScore(1).build());

           return new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);

        }else if("2".equals(pid)){
            map.put("msg","执行失败");
//            return new ResponseEntity<Map<String,Object>>(map, HttpStatus.valueOf(40001));
            return new ResponseEntity<Map<String,Object>>(map, HttpStatus.LENGTH_REQUIRED);


        }
        return new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
    }

}
